package com.dongdong.android.demo.ui.downloadlist;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.dongdong.android.demo.R;
import com.dongdong.android.framework.base.BaseActivity;
import com.dongdong.android.framework.download.DownloadManager;
import com.dongdong.android.framework.download.DownloadStatusListener;
import com.dongdong.android.framework.download.FileInfo;
import com.dongdong.android.framework.download.StatusMode;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * @author dongdongzheng
 */
public class DownloadListActivity extends BaseActivity {

    @BindView(R.id.down_load_list_btn_add_item)
    Button mBtnAddItem;


    @BindView(R.id.down_load_list_recycle_list)
    RecyclerView mRecycleList;


    private List<FileInfo> list;
    private DownloadAdapter adapter;
    private HashMap<String, Integer> positionMap;
    String[] strpath = {"http://imtt.dd.qq.com/16891/2D54AB7D3909BFCE37550C055212E256.apk",
            "http://imtt.dd.qq.com/16891/5C64C9AEF9046D107B47887E7D32260B.apk",
            "http://imtt.dd.qq.com/16891/AA02DF7EB37E9188CCBFAB0DC9E35B71.apk",
            "http://imtt.dd.qq.com/16891/0B42E2147FF8492CCD766D064E1F4076.apk"};

    @Override
    public int getLayoutId() {
        return R.layout.download_list_activity;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        if (list == null) {
            list = new ArrayList<>();
        } else {
            list.clear();
        }
        if (positionMap == null) {
            positionMap = new HashMap<>();
        } else {
            positionMap.clear();
        }

        adapter = new DownloadAdapter(this.mContext, list, adapterlistener);
        mRecycleList.setLayoutManager(new LinearLayoutManager(this.mContext));
        mRecycleList.setAdapter(adapter);

    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        mBtnAddItem.setOnClickListener(this);

    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.down_load_list_btn_add_item:
                for (int i = 0; i < strpath.length; i++) {
                    FileInfo info = new FileInfo();
                    info.setProgress(0);
                    info.setStatus(StatusMode.INIT);
                    info.setUrl(strpath[i]);
                    list.add(info);
                }
                adapter.notifyDataSetChanged();
                break;
            default:
                break;
        }

    }

    View.OnClickListener adapterlistener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = (int) v.getTag();
            positionMap.put(list.get(position).getUrl(), position);
            switch (list.get(position).getStatus()) {
                case INIT:
                    DownloadManager.getInstance(mContext).download(list.get(position),
                            downloadStatusListener);
                    break;
                case RUN:
                    DownloadManager.getInstance(mContext).pause(list.get(position));
                    break;
                case PAUSE:
                    DownloadManager.getInstance(mContext).start(list.get(position));
                    break;
                default:
                    break;
            }
//            adapter.notifyItemChanged(position,"1234");
        }
    };

    DownloadStatusListener downloadStatusListener = new DownloadStatusListener() {
        @Override
        public void success(FileInfo info) {
            if (positionMap.containsKey(info.getUrl())) {
                list.get(positionMap.get(info.getUrl())).setStatus(StatusMode.SUCCESS);
                list.get(positionMap.get(info.getUrl())).setProgress(100);
                adapter.notifyDataSetChanged();
            }
        }

        @Override
        public void fail(String url, String strfail) {

        }

        @Override
        public void error(String url, String e) {

        }

        @Override
        public void progress(String url, int progress) {
            if (positionMap.containsKey(url)) {
                list.get(positionMap.get(url)).setStatus(StatusMode.RUN);
                list.get(positionMap.get(url)).setProgress(progress);
                adapter.notifyDataSetChanged();
            }

        }

        @Override
        public void pause(String url) {
            if (positionMap.containsKey(url)) {
                list.get(positionMap.get(url)).setStatus(StatusMode.PAUSE);
                adapter.notifyDataSetChanged();
            }
        }

        @Override
        public void cancle(String url) {
            if (positionMap.containsKey(url)) {
                list.get(positionMap.get(url)).setStatus(StatusMode.CANCLE);
                adapter.notifyDataSetChanged();
            }
        }
    };

}
